<template>
  <div class="login">
    <div class="login-box">
      <div class="top">
        <div class="logo"><img src="https://xiaoxiangai.oss-cn-beijing.aliyuncs.com/2024/05/07/logo.e5facb4.png1"
               alt=""></div>
      </div>
      <div class="mid">
        <el-form :model="dataForm"
                 :rules="dataRule"
                 ref="dataForm"
                 @keyup.enter.native="dataFormSubmit()"
                 label-position="right"
                 label-width= '100px'
                 status-icon>
          <el-form-item prop="userName" label="用户名">
            <el-input class="info"
                      v-model="dataForm.userName"
                      placeholder="帐号"></el-input>
          </el-form-item>
          <el-form-item prop="mobile" label="手机号">
            <el-input class="info"
                      v-model="dataForm.mobile"
                      placeholder="手机号"></el-input>
          </el-form-item>
          <el-form-item prop="checkCode" label="验证码">
            <el-input v-model="dataForm.checkCode"
                      placeholder="验证码">
              <span slot="append" style="border:0;color:#1F87E8;background-color:rgba(0,0,0,0);width:100%;height:100%;" v-if="!isPending" @click="getCheckCode">获取验证码</span>
              <span slot="append" style="border:0;color:#606266;background-color:rgba(0,0,0,0);width:100%;height:100%;" v-if="isPending">{{remainingTime}}</span>
            </el-input>
          </el-form-item>
          <el-form-item prop="password" label="密码">
            <el-input class="info"
                      v-model="dataForm.password"
                      type="password"
                      placeholder="不能是纯数字"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword" label="确认密码">
            <el-input class="info"
                      v-model="dataForm.confirmPassword"
                      type="password"
                      placeholder="确认密码"></el-input>
          </el-form-item>
          <!-- <el-form-item label="密码">
            <div class="form-footer">
            <el-checkbox v-model="savePasswotd">三天内自动登录</el-checkbox>
            <span>忘记密码?</span>
            </div>
          </el-form-item> -->
          <el-form-item>
            <div class="item-btn"><input type="button"
                     value="重置密码"
                     @click="dataFormSubmit()" style="display: inline-block;width: 100px;">
                     <span style="padding-left:30px"><a href="/login">想起密码，去登陆</a></span> </div>
          </el-form-item>
        </el-form>
      </div>

      <div class="bottom">Copyright © 2020 POS收银系统线上服务平台</div>
    </div>
  </div>
</template>

<script>
import { getUUID } from '@/utils'
import { isMobile } from '@/utils/validate'

export default {
  data () {
    var validateUserName = (rule, value, callback) =>{
      this.$http({
        url: this.$http.adornUrl('/sys/user/checkUserByUserName'),
        method: 'POST',
        params: this.$http.adornParams({
          username: this.dataForm.userName,
        })
      }).then(({data}) =>{
        if (data) {
          callback()
        }else{
          callback(new Error('该账号不存在'))
        }
      })
    }

    var validateMobile = (rule,value,callback) =>{
      if (!isMobile(value)) {
        callback(new Error('手机号格式错误'))
      } else{
        console.log('Running');
        // callback()
        this.$http({
          url: this.$http.adornUrl('/sys/user/checkUserByPhone'),
          method: 'post',
          params: this.$http.adornParams({
            mobile: this.dataForm.mobile,
            // code: this.dataForm.checkCode
          })
        }).then(({data}) =>{
          if (data == false) {
            callback(new Error('该手机号未注册，不能找回密码'))
          }else{
            callback()
          }
          // if (data == '000') {
          //   callback(new Error('检验次数次数过多，请稍后重试'))
          // }else if (data == '111') {
          //   callback(new Error('验证码错误'))
          // }else if (data == '222') {
          //   callback(new Error('验证码已过期'))
          // }else if (data == '999'){
          //   this.codeIsLegal = true
            callback()
          // }
        })
      }
    }

    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      }else if (/^\d+$/.test(value)) {
        callback(new Error('密码不能为纯数字'))
      } else {
        if (this.dataForm.confirmPassword !== '') {
          this.$refs.dataForm.validateField('confirmPassword');
        }
        callback();
      }
    };
    var validateConfirmPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.dataForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    var validatecheckCode = (rule, value, callback) => {
      if (this.checkcodeRunning) {
        return
      }
      this.checkcodeRunning = true
      if (this.codeIsLegal) {
        callback()
      }else{
        this.$http({
          url: this.$http.adornUrl('/p/sms/checkRetrieve'),
          method: 'get',
          params: this.$http.adornParams({
            mobile: this.dataForm.mobile,
            code: this.dataForm.checkCode
          })
        }).then(({data}) =>{
          if (data == '000') {
            callback(new Error('检验次数次数过多，请稍后重试'))
          }else if (data == '111') {
            callback(new Error('验证码错误'))
          }else if (data == '222') {
            callback(new Error('验证码已过期'))
          }else if (data == '999'){
            this.codeIsLegal = true
            callback()
          }
        })
      }
      this.checkcodeRunning = false
    };

    return {
      codeIsLegal: false,
      remainingTime: 60,
      savePasswotd:false,
      isPending: false,
      checkcodeRunning: false,
      dataForm: {
        userName: '',
        mobile: '',
        password: '',
        confirmPassword: ''
      },
      dataRule: {
        userName: [
          { required: true, message: '帐号不能为空', trigger: 'blur' },
          { validator: validateUserName, trigger: 'blur'}
        ],
        mobile: [
          { required: true, message: '帐号不能为空', trigger: 'blur' },
          { validator: validateMobile,trigger: 'blur'}
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator:validatePass, trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          {validator: validateConfirmPass}
        ],
        checkCode: [
          { required: true, message: '请填写验证码', trigger: 'blur' },
          { validator: validatecheckCode, trigger:'blur'}
        ],
      },
    }
  },
//   created () {
//     this.getCaptcha()
//   },
  methods: {
    // 提交表单
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/sys/user/retrievePassword'),
            method: 'put',
            data: this.$http.adornData({
              username: this.dataForm.userName,
              mobile: this.dataForm.mobile,
              password: this.dataForm.password,
            })
          }).then(({ data }) => {
            // if (this.savePasswotd) {
            //   this.$cookie.set('Authorization', 'bearer' + data.access_token,3)
            // }else{
            //   this.$cookie.set('Authorization', 'bearer' + data.access_token)
            // }
            this.$router.replace({ name: 'login' })
            this.$message({
              message: '修改成功，请登录',
              type: 'success'
            })
          }).catch(() => {
            // this.getCaptcha()
          })
        }
      })
    },
    getCheckCode(){
      if (!this.dataForm.mobile) {
        this.$message.error('请先输入手机号')
        return
      }
      this.sendMsg()
      this.isPending = true
      var conter = setInterval(() => {
        this.remainingTime-=1
      }, 1000);
      setTimeout(() =>{
        clearInterval(conter)
        this.isPending = false
        this.remainingTime = 60
      },60000)
    },

    sendMsg(){
      console.log('running');
      this.$http({
        url: this.$http.adornUrl('/p/sms/retrievePassword'),
        method: 'get',
        params: this.$http.adornParams({
          mobile: this.dataForm.mobile
        })
      }).then((res) =>{
          console.log(res);
      }).catch(() =>{
        this.remainingTime = 60
        this.isPending = false
      })
    },
  }
}
</script>

<style lang="scss">
.login {
  width: 100%;
  height: 100%;
  background: url(~@/assets/img/login-bg.png) no-repeat;
  background-size: cover;
  position: fixed;
}
.login .login-box {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  height: 100%;
  padding-top: 10%;
}
.login .login-box .top {
  margin-bottom: 30px;
  text-align: center;
}
.login .login-box .top .logo {
  font-size: 0;
}
.login .login-box .top .company {
  font-size: 16px;
  margin-top: 10px;
}
.login .login-box .mid {
  font-size: 14px;
}
.login .login-box .mid .item-btn {
  margin-top: 20px;
}
.login .login-box .mid .item-btn input {
  border: 0;
  width: 100%;
  height: 40px;
  box-shadow: 0;
  background: #1f87e8;
  color: #fff;
  border-radius: 3px;
}
.info {
  width: 410px;
}
.login-captcha {
  height: 40px;
}
.login .login-box .bottom {
  position: absolute;
  bottom: 10%;
  width: 100%;
  color: #999;
  font-size: 12px;
  text-align: center;
}
.form-footer{
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding-right: 20px;
  // font-size: 10px;
  color: #B7B8B7;
}
.el-checkbox__label {
  color: #B7B8B7;
}
</style>
